﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Wpf_ClientLeger.BusinessObject;
using Wpf_ClientLeger.ServicePreparation;

namespace Wpf_ClientLeger.IHM
{
    public partial class ValidationBP : Window
    {

        Parametres paramAppli;
        public User currentUser;
        public ServicePreparationClient ServicePrepa;
        int nbBpATraiter;
        int nbBlGeneres;
        int bpNo;
        BPDTO bp;

        public ValidationBP(Parametres param, User user)
        {
            InitializeComponent();

            ServicePrepa = new ServicePreparationClient();
            this.currentUser = user;
            this.paramAppli = param;
            this.lbl_error.Content = "";
            this.label_ok.Content = "";

            //recherche du nombre de BP préparé(s) par l'utilisateur
            //this.nbBpATraiter = this.ServicePrepa.CalculerBpAValider(user.idUser);
            //this.label_nbBP.Content = this.nbBpATraiter.ToString();
            this.txt_gencod.Focus();
        }

        //bouton validation --> génération des BL + MAJ des statuts Commandes et BP
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.lbl_error.Content = "";
            this.label_ok.Content = "";
            try
            {
                bpNo = int.Parse(this.txt_gencod.Text);
            }
            catch(Exception ex)
            {
                bpNo = 0;    
            }

            if (bpNo > 0)
            {
                bp = this.ServicePrepa.ChargerBP(bpNo);
                if (bp.idBP > 0)
                {
                    //interogation si toutes les lignes du BP ont été saisies
                    if (this.ServicePrepa.AllBpLinesReady(bp.idBP))
                    {
                        //si oui on valide en BL
                        this.ServicePrepa.ValiderBpEnBl(bp.idBP);
                        this.txt_gencod.Text = "";
                        this.label_ok.Content = "BL " + bp.idBP.ToString() + " généré";
                    }
                    else
                    {
                        //si non erreur
                        this.lbl_error.Content = "PREPARATION PARTIELLE";
                    }
                }
                else
                {
                    this.lbl_error.Content = "BP NON TROUVE";
                }
            }
            else
            {
                this.lbl_error.Content = "ERREUR FORMAT";
            }

            this.txt_gencod.Focus();
            this.txt_gencod.SelectAll();

        }

        private void bt_finSaisie_Click(object sender, RoutedEventArgs e)
        {
            Preparation_Menu menuPrepa = new Preparation_Menu(paramAppli, currentUser);
            menuPrepa.Show();
            this.Close();
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                button1_Click(sender, e);
            }
        }
    }
}
